//
//  UIViewController+PresentBack.m
//  bgms_p
//
//  Created by xiao.zhao@lachesis-mh.com on 16/2/26.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "UIViewController+PresentBack.h"

#import <objc/runtime.h>

static const void *PresentViewController = @"PresentViewController";

@implementation UIViewController (PresentBack)

//+(void)load
//{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        SEL originSel = @selector(presentViewController:animated:completion:);
//        SEL replaceSel = @selector(lx_presentViewController:animated:completion:);
//        
//        Method originMethod = class_getInstanceMethod([self class], originSel);
//        Method replaceMethod = class_getInstanceMethod([self class], replaceSel);
//        
//        BOOL isAdd = class_addMethod([self class], originSel, method_getImplementation(replaceMethod), method_getTypeEncoding(replaceMethod));
//        if(isAdd)
//        {
//            class_replaceMethod([self class], replaceSel, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
//        }
//        else
//        {
//            method_exchangeImplementations(originMethod, replaceMethod);
//        }
//    });
//}
//
//- (void)lx_presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion
//{
//    [self lx_presentViewController:viewControllerToPresent animated:flag completion:completion];
//    
//    self.lx_presentVC = viewControllerToPresent;
//    if([viewControllerToPresent isKindOfClass:[UINavigationController class]])
//    {
//        UINavigationController *navVC = (UINavigationController *)viewControllerToPresent;
//        
//        if(!navVC.topViewController.navigationItem.leftBarButtonItem)
//        {
//            navVC.topViewController.navigationItem.leftBarButtonItem = [self leftBackItem];
//        }
//    }
//}
//
//-(UIBarButtonItem *)leftBackItem
//{
//    UIImage *navBackImage = nil;
//    if([self.lx_presentVC isKindOfClass:[LXWhiteColorNavigationController class]])
//    {
//        navBackImage = [UIImage imageNamed:@"icon_common_back"];
//    }
//    else
//    {
//        navBackImage = [UIImage imageNamed:@"icon_common_whiteBack"];
//    }
//    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:[navBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBackAction)];
//    
//    return backItem;
//}

-(void)leftBackAction
{
    [self.lx_presentVC dismissViewControllerAnimated:YES completion:nil];
}

-(UIViewController *)lx_presentVC
{
    return objc_getAssociatedObject(self, PresentViewController);
}

-(void)setLx_presentVC:(UIViewController *)lx_presentVC
{
    objc_setAssociatedObject(self, PresentViewController, lx_presentVC, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message singleTitle:(NSString *)singleTitle handle:(void (^)(NSInteger))handle
{
    [self showAlertWithTitle:title message:message leftTitle:singleTitle rightTitle:nil handle:handle];
}

-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle handle:(void (^)(NSInteger))handle
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:leftTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if(handle) handle(0);
    }];
    [alert addAction:leftAction];
    if (rightTitle)
    {
        UIAlertAction *rightAction = [UIAlertAction actionWithTitle:rightTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if(handle) handle(1);
        }];
        [alert addAction:rightAction];
    }
    
    [self presentViewController:alert animated:YES completion:nil];
}

@end
